<template >
  <div class="homeSetUp-container">
    <breadcrumb style="position: absolute;width:100%;"></breadcrumb>
    <div class="content">
      <div class="query">
        <div class="margin_right_20">
          会员卡类型:&nbsp;
          <el-select v-model="all" style="width: 100px;">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </div>
        <div class="margin_right_20">
          优惠券状态:&nbsp;
          <el-select v-model="all1" style="width: 100px;">
            <el-option
              v-for="item in options2"
              :key="item.value2"
              :label="item.label"
              :value="item.value2"
            ></el-option>
          </el-select>
        </div>
        <div class="btn">
          <el-button type="primary" icon="el-icon-search" @click="get_goodsList(1)">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="dialogVisible=true">新增</el-button>
        </div>
      </div>
      <div class="table">
        <el-table :data="couponList" style="width: 100%" height="598">
          <el-table-column prop="cardName" label="会员卡类型"></el-table-column>
          <el-table-column prop="couponName" label="优惠券名称"></el-table-column>
          <el-table-column prop="typeId" label="优惠券类型"></el-table-column>
          <el-table-column prop="couponPrice" label="优惠券面额"></el-table-column>
          <el-table-column prop="effictTime" label="优惠券有效期"></el-table-column>
          <el-table-column prop="grantCount" label="优惠券发放数量"></el-table-column>
          <el-table-column prop="useCount" label="优惠券已使用数量"></el-table-column>
          <el-table-column prop="status" label="优惠券状态"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status == '禁用' ? true:false"
                type="text"
                size="small"
                @click="status_modify(scope.$index,scope.row.id,scope.row.status)"
              >启用</el-button>
              <el-button
                v-else
                type="text"
                size="small"
                @click="status_modify(scope.$index,scope.row.id,scope.row.status)"
              >禁用</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :currentPage="currentPage"
          :page="page"
          :total="total"
          :pageSize="pageSize"
          @change-current="currentChange"
          @change-size="sizeChange"
        ></pagination>
        <el-dialog :visible.sync="dialogVisible" width="23%">
          <el-form label="form" v-model="form">
            <el-form-item label="优惠券：" label-position="right" label-width="120px">
              <el-button type="primary" style="width:110px" @click="select_coupon">请选择优惠券</el-button>
            </el-form-item>
            <el-form-item label-width="120px" label="会员卡类型：">
              <el-select v-model="form.cardId" style="width:70%">
                <el-option label="月卡" value="1"></el-option>
                <el-option label="季卡" value="2"></el-option>
                <el-option label="年卡" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <div class="btn">
              <el-button @click="dialogVisible=false" size="small" style="width:120px">取 消</el-button>
              <el-button type="primary" @click="addCoupon" size="small" style="width:120px">确 定</el-button>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import breadcrumb from "../../components/breadcrumb";
import pagination from "../../components/page";
import { coupon_list, coupon_delete } from "../../api/getData.js";
export default {
  components: { breadcrumb, pagination },
  data() {
    return {
      all: "",
      all1: "",
      coupnId: "",
      isFlag: true,
      active: [],
      currentPage: 1,
      total: 0,
      time: "",
      couponList: [],
      dialogVisible: false,
      dialogVisible1: false,
      options1: [
        {
          value: "",
          label: "所有"
        },
        {
          value: "1",
          label: "月卡"
        },
        {
          value: "2",
          label: "季卡"
        },
        {
          value: "3",
          label: "年卡"
        }
      ],
      options2: [
        {
          value2: "",
          label: "所有"
        },
        {
          value2: "0",
          label: "启用"
        },
        {
          value2: "1",
          label: "禁用"
        }
      ],
      options3: [
        {
          value: "",
          label: "所有"
        },
        {
          value: "1",
          label: "全场通用"
        },
        {
          value: "2",
          label: "部分活动通用"
        },
        {
          value: "3",
          label: "部分商品类型通用"
        },
        ,
        {
          value: "4",
          label: "部分活动商品类型通用"
        },
        ,
        {
          value: "5",
          label: "部分商品可用"
        }
      ],

      value: "",
      value1: "",
      value2: "",
      value3: "",
      value4: "",
      value5: "",
      page: 1,
      pageSize: 10,
      dialogFormVisible: false,
      form: []
    };
  },
  props: ["positionsId"],
  created() {
    if (localStorage.getItem("pageMsg")) {
      let pageMsg = JSON.parse(localStorage.getItem("pageMsg"));
      this.value = pageMsg.value;
      this.value1 = pageMsg.value1;
      this.value2 = pageMsg.value2;
      this.value3 = pageMsg.value3;
      this.value4 = pageMsg.value4;
      this.value5 = pageMsg.value5;
      this.currentPage = pageMsg.currentPage;
      this.pageSize = pageMsg.pageSize;
      localStorage.removeItem("pageMsg");
    }
    this.get_goodsList();
    let couponMsg = localStorage.getItem("couponMsg") || "";
    if (couponMsg) {
      localStorage.removeItem("couponMsg");
      this.couponMsg = JSON.parse(couponMsg);
      this.coupnId = this.couponMsg.couponId;
      this.dialogVisible = true;
      this.get_goodsList();
    }
  },
  methods: {
    select_coupon() {
      localStorage.setItem(
        "formData",
        JSON.stringify({
          form: this.form
        })
      );
      this.$router.push("/selectCoupon_");
    },
    addCoupon() {
      if (!this.coupnId) {
        return this.$message({
          type: "warning",
          message: "请选择优惠券"
        });
      }
      if (!this.form.cardId) {
        return this.$message({
          type: "warning",
          message: "请选择会员卡类型"
        });
      }

      let data1 = {
        cardId: this.form.cardId,
        coupnId: this.coupnId
      };

      this.$axios.post("/member/coupon/add", data1).then(result => {
        if (result.data.status == 0) {
          if (result.data.data.data == 1) {
            this.get_goodsList();
            return this.$message({
              type: "success",
              message: "添加成功"
            });
          } else {
            return this.$message({
              type: "warning",
              message: "添加失败"
            });
          }
        }
      });
      this.dialogVisible = false;
    },
    sizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.get_goodsList();
    },
    currentChange(val) {
      this.currentPage = val;
      this.get_goodsList();
    },
    status_modify(i, id, status) {
      this.$confirm("此操作将修改状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var goodsStatus = "";
          if (status == "启用") {
            goodsStatus = 1;
          } else {
            goodsStatus = 0;
          }

          let data = {
            cardName: this.couponList[i].cardName,
            couponName: this.couponList[i].couponName,
            couponPrice: this.couponList[i].couponPrice,
            grantCount: this.couponList[i].grantCount,
            useCount: this.couponList[i].useCount,
            status: goodsStatus,
            id,
            cardId: this.couponList[i].cardId,
            couponId: this.couponList[i].couponId
          };

          this.$axios.post("/member/coupon/update", data).then(result => {
            if (result.data.status == 0) {
              if (result.data.data.data == 1) {
                if (status == "启用") {
                  this.$message({
                    type: "success",
                    message: "禁用成功"
                  });
                } else {
                  this.$message({
                    type: "success",
                    message: "启用成功"
                  });
                }

                this.couponList.forEach(item => {
                  if (item.id == id) {
                    item.status = item.status == "禁用" ? "启用" : "禁用";
                  }
                });
              } else {
                this.$message({
                  type: "warning",
                  message: "操作失败"
                });
              }
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改"
          });
        });
    },
    jump_goods_detail(id) {
      localStorage.setItem("path", JSON.stringify(this.$route.path));

      localStorage.setItem(
        "pageMsg",
        JSON.stringify({
          value: this.value,
          value1: this.value1,
          value2: this.value2,
          currentPage: this.currentPage,
          pageSize: this.pageSize
        })
      );

      this.$router.push("goodsDetail?id=" + id);
    },
    get_goodsList(currentPage) {
      if (currentPage) {
        this.currentPage = currentPage;
      }
      this.couponList = [];
      // let data = {
      //   current: this.currentPage,
      //   size: this.pageSize
      // };
      let data =
        "size=" +
        this.pageSize +
        "&current=" +
        this.currentPage +
        "&cardStatus=" +
        this.all +
        "&couponType=" +
        this.all1;

      this.$axios
        .post(
          "/member/coupon/list?cardStatus=" +
            this.all +
            "&couponType=" +
            this.all1,
          data
        )
        .then(res => {
          if (res.data.status == 0 && res.data.data.records.length != 0) {
            this.couponList = res.data.data.records;
            this.couponList.forEach(item => {
              item.status = item.status == 0 ? "启用" : "禁用";
              item.typeId = this.options3[item.typeId].label;
            });

            this.total = res.data.data.total;
            this.page = Math.ceil(this.total / this.pageSize);
          }
        });
    }
  }
};
</script>


<style lang="less" >
.homeSetUp-container {
  @import "../../style/table.less";
  @import "../../style/content.less";
  display: flex;
  flex-flow: column;
  position: relative;
  height: 100%;
  .el-table .cell {
    white-space: pre-line !important;
  }
  .content {
    .query {
      .el-form-item {
        margin-bottom: 8px !important;
      }
      .start_time {
        display: flex;
        align-items: center;
      }
      .end_time {
        display: flex;
        align-items: center;
      }
      .btn {
        .el-button {
          width: 70px;
        }
      }
    }
  }
}
</style>

